package com.ys.erp.entity.data.bean;

import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lwj
 * @date 2025/7/31
 */

@Data
@TableName(value = "t_hua_bei_order")
public class HuaBeiOrder {
    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 订单编号
     */
    @TableField(value = "order_no")
    private String orderNo;


    @TableField(value = "user_id")
    private Long userId;

    @TableField(value = "cat_code")
    private String catCode;

    @TableField(value = "is_in_moneyed")
    private Integer isInMoneyed;

    @TableField(value = "period")
    private Integer period;

    @TableField(value = "period_money")
    private Long periodMoney;

    /**
     * 来源订单号
     */
    @TableField(value = "source_order_no")
    private String sourceOrderNo;

    /**
     * 订单编号
     */
    @TableField(value = "order_title")
    private String orderTitle;

    /**
     * 产品吗
     */
    @TableField(value = "product_code")
    private String productCode;

    /**
     * 订单扩展信息，json格式
     */
    @TableField(value = "ext_body")
    private String extBody;

    /**
     * 商品信息，json数组
     */
    @TableField(value = "goods_detail")
    private String goodsDetail;

    /**
     * 订单金额
     */
    @TableField(value = "order_money")
    private Long orderMoney;

    /**
     * 超时时间
     */
    @TableField(value = "expire_time")
    private LocalDateTime expireTime;

    /**
     * 支付连接
     */
    @TableField(value = "pay_url")
    private String payUrl;

    /**
     * 交易码
     */
    @TableField(value = "trade_no")
    private String tradeNo;

    /**
     * 员工名称
     */
    @TableField(value = "emp_name")
    private String empName;

    /**
     * 店铺编码
     */
    @TableField(value = "shop_code")
    private String shopCode;

    /**
     * INIT:初始化，WAIT_BUYER_PAY:交易创建，TRADE_SUCCESS：交易成功，TRADE_FINISHED：交易完结，TRADE_CLOSED：交易关闭
     */
    @TableField(value = "order_status")
    private String orderStatus;

    /**
     * 关闭时间
     */
    @TableField(value = "pay_time")
    private LocalDateTime payTime;

    /**
     * 创建时间
     */
    @TableField(value = "init_time")
    private LocalDateTime initTime;

    /**
     * 成功时间
     */
    @TableField(value = "success_time")
    private LocalDateTime successTime;

    /**
     * 完结时间
     */
    @TableField(value = "ok_time")
    private LocalDateTime okTime;

    @TableField(value = "close_time")
    private LocalDateTime closeTime;

    @TableField(value = "version")
    @Version
    private Integer version;

    @TableField(value = "create_time")
    private LocalDateTime createTime;

    @TableField(value = "update_time", update = "now()")
    private LocalDateTime updateTime;

    public String getRandOrderNo() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        return  "HB"
            + calendar.get(Calendar.YEAR)
            + (month >= 10 ? String.valueOf(month) : "0" + month)
            + (day >= 10 ? String.valueOf(day) : "0" + day)
            + calendar.get(Calendar.HOUR_OF_DAY)
            + calendar.get(Calendar.MINUTE)
            + calendar.get(Calendar.SECOND)
            + ((int)((Math.random()*9+1)*10000000));
    }

    public String getOnlyUserNo() {
        if (this.userId == null) {
            return null;
        }
        return this.userId + "-" + this.getCatCode();
    }

    @AllArgsConstructor
    @Getter
    public enum Enums {
        INIT,
        WAIT_BUYER_PAY,
        TRADE_SUCCESS,
        TRADE_FINISHED,
        TRADE_CLOSED
    }

    public static final Map<String, String> ENUM_MAP = new HashMap<>();

    static {
        ENUM_MAP.put(Enums.INIT.name(), "初始化");
        ENUM_MAP.put(Enums.WAIT_BUYER_PAY.name(), "交易创建");
        ENUM_MAP.put(Enums.TRADE_SUCCESS.name(), "交易成功");
        ENUM_MAP.put(Enums.TRADE_FINISHED.name(), "交易完结");
        ENUM_MAP.put(Enums.TRADE_CLOSED.name(), "交易关闭");
    }

}
